function plot_datalogger_file(filename, start_date, end_date)
% DATALOGGER - Display data graph from a log file.
%
% Usage:
%  plot_datalogger_file(<mat_filename>,<initial_date>,<end_date>) 
%  date format 'yyyy/mm/dd HH:MM:SS'
%  if empty ('') then it uses the default date.
%
%   plot_datalogger_file('2010_07_16_no03.txt_DL.mat', 
%       '2010/07/16 10:00:00','2010/07/16 10:10:00')
%

%% cleaning

%% load data from file
values = [];
load(filename);
disp(['Loaded:', filename]);


%% filtering data to period

default_start='2010/01/01 00:00:01';
default_end='2012/01/01 00:00:01';

if (strcmp(start_date,''))
    start_date = default_start;
end
if (strcmp(end_date,''))
    end_date = default_end;
end
   
%start_date=datenum('2010/07/16 08:24:00','yyyy/mm/dd HH:MM:SS');
%end_date=datenum('2010/07/16 12:00:00','yyyy/mm/dd HH:MM:SS');
start_date=datenum(start_date,'yyyy/mm/dd HH:MM:SS');
end_date=datenum(end_date,'yyyy/mm/dd HH:MM:SS');

data = filter_data(start_date, end_date, values.data);


%% Plot data
h = figure ('Name', ['Datalogger: ', filename] );
hold on;
ylabel('Voltage');

color = get_a_new_color();
plot(data(:,1), data(:,2), 'color',color);
legend ('CO2 concentration'); %legend should be added after plots
datetick('x','HH:MM:SS','keepticks')


function selected_data = filter_data(start_date, end_date, data)

start_index = 1;
end_index = length(data);


%find first greater date
for i =1:end_index
    date = data(i,1);
    if (date>start_date)
        start_index = i;
        break;
    end
end
disp(['Plotting start date:', datestr(date)]);

for i =start_index:end_index
    date = data(i,1);
    if (date>end_date)
        end_index = i-1;
        break;
    end
end
disp(['Plotting end date:', datestr(data(end_index ,1))]);

selected_data = [data(start_index:end_index,1) data(start_index:end_index,2)];

